<template>
  <div class="articles">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="getList"
    >
      <van-cell
        v-for="(item, i) in list"
        :key="i"
        :to="{ name: 'article', params: { id: item.id, article: item } }"
      >
        <!--        这里因为写了:id的是有动态参数的, 所以会展示出id, 但是article我们没有写动态参数, 所以就不会展示出article-->
        <div class="cell-header">
          <van-image
            width="30"
            height="30"
            :src="
              item.original_status.user.photo_domain +
              item.original_status.user.profile_image_url.split(',')[0]
            "
          />
          <div class="right-header">
            <div class="author">
              {{ item.original_status.user.screen_name }}
            </div>
            <div class="pubtime">{{ item.original_status.timeBefore }}</div>
          </div>
        </div>
        <p
          v-html="item.original_status.description"
          @click="goArticle(item.id, item)"
        ></p>
        <div class="cell-footer">
          <div class="item">
            <van-icon name="share-o" />
            <div class="num">{{ item.original_status.retweet_count }}</div>
          </div>
          <div class="item">
            <van-icon name="chat-o" />
            <div class="num">{{ item.original_status.reply_count }}</div>
          </div>
          <div class="item">
            <van-icon name="good-job-o" />
            <div class="num">{{ item.original_status.like_count }}</div>
          </div>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { getArticles } from "@/api/index.js";
import { Cell, Image as VanImage, Icon, List } from "vant";

export default {
  name: "Articles03",
  data() {
    return {
      page: 1,
      list: [],
      finished: false,
      loading: false,
    };
  },
  methods: {
    async getList() {
      this.loading = true;
      let result = await getArticles({ page: this.page });
      this.loading = false;
      if (result.result && result.result.indexOf("已加载") !== -1) {
        this.finished = true;
      } else {
        this.list = this.list.concat(result.items);
        this.page++;
      }
    },
    goArticle(id, item) {
      let articles = {};
      articles[id] = item;
      // 如果localStorage中存在articles的话, 就先删除然后再设置
      // 不存在的话就直接设置
      if (localStorage.getItem("articles")) {
        localStorage.removeItem("articles");
        localStorage.setItem("articles", JSON.stringify(articles));
        this.$forceUpdate();
      } else {
        localStorage.setItem("articles", JSON.stringify(articles));
        this.$forceUpdate();
      }
    },
  },
  async mounted() {
    this.getList();
  },
  components: {
    "van-cell": Cell,
    "van-image": VanImage,
    "van-icon": Icon,
    "van-list": List,
  },
};
</script>

<style lang="less" scoped>
.cell-header {
  display: flex;
  height: 1rem;
  .right-header {
    font-size: 0.3rem;
    line-height: 0.4rem;
    padding-left: 0.2rem;
    .pubtime {
      color: #999;
    }
  }
}

.cell-footer {
  display: flex;
  height: 0.5rem;
  align-items: center;
  .item {
    display: flex;
    font-size: 0.3rem;
    width: 1.5rem;
    height: 0.5rem;
    align-items: center;
    .num {
      padding: 0 0.1rem;
    }
  }
}
.finish {
  text-align: center;
  color: #bbb;
  height: 1rem;
  line-height: 1rem;
  font-size: 0.4rem;
  background-color: #efefef;
}
</style>
